package com.shop.exception;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author : WanS
 * @date : 20:27 2020/11/30
 * 返回给前端的统一类
 */
@Setter
@Getter
public class Result<T> implements Serializable {
    public static final int SUCCESS_CODE = 200;
    public static final String SUCCESS_MESSAGE = "操作成功";
    public static final int ERROR_CODE = 500;
    public static final String ERROR_MESSAGE = "系统故障";

    private int code;
    private String msg;
    private T data;

    public Result() {
    }
    private Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public static <T>Result<T> success(T data){
        return new Result<>(SUCCESS_CODE,SUCCESS_MESSAGE,data);
    }
    public static Result error(CodeMsg codeMsg){
        return new Result(codeMsg.getCode(),codeMsg.getMsg(),null);
    }
    public static Result defaultError(){
        return new Result(ERROR_CODE,ERROR_MESSAGE,null);
    }
}
